package models

import "gorm.io/gorm"

type Users struct {
	gorm.Model
	Username string `gorm:"type:varchar(30)"json:"username"`
	Password string `gorm:"type:varchar(225)"json:"password"`
}

type Usersrolegxb struct {
	gorm.Model
	Uid int `gorm:"type:char(10)"json:"uid"`
	Rid int `gorm:"type:char(10)"json:"rid"`
}

type Role struct {
	gorm.Model
	Rolename string `gorm:"type:varchar(30)"json:"rolename"`
}

type Quanrolegxb struct {
	gorm.Model
	Qid int `gorm:"type:char(10)"json:"qid"`
	Rid int `gorm:"type:char(10)"json:"rid"`
}
type Quan struct {
	gorm.Model
	Quanname string `gorm:"type:varchar(30)"json:"quanname"`
}

type Companss struct {
	gorm.Model
	Comname string `gorm:"type:varchar(30)"json:"comname"`
}

type Orders struct {
	gorm.Model
	Ordername string `gorm:"type:varchar(30)"json:"ordername"`
	Tid       int    `gorm:"type:char(10)"json:"tid"`
	Cid       int    `gorm:"type:char(10)"json:"cid"`
}

type Tyeps struct {
	gorm.Model
	Tyepname string `gorm:"type:varchar(30)"json:"tyepname"`
}

func (*Users) DataAdd(username string, users *Users) error {
	return DB.Where("username = ?", username).First(&users).Error
}

func (*Quanrolegxb) AddData(data *Quanrolegxb) (int, error) {
	tx := DB.Begin()
	strstr := tx.Debug().Create(&data)
	if strstr.Error != nil {
		tx.Rollback()
		return 0, strstr.Error
	}
	tx.Commit()
	return int(data.ID), nil
}
func (*Role) List(uid int) (list []Role, err error) {
	err = DB.Table("roles").
		Joins(" LEFT JOIN usersrolegxbs ON usersrolegxbs.rid = roles.id").
		Where("usersrolegxbs.uid =?", uid).
		Find(&list).Error
	return list, err
}
func (*Quan) QuanrolegxbListss(rid int) (list []Quan, err error) {
	err = DB.Table("quans").
		Joins(" LEFT JOIN quanrolegxbs on quanrolegxbs.qid=quans.id").
		Where("quanrolegxbs.rid=?", rid).
		Find(&list).Error
	return list, err
}

func (*Companss) Addiseds(data *Companss) (int, error) {
	tx := DB.Begin()
	serlrt := DB.Debug().Create(&data)
	if serlrt.Error != nil {
		tx.Rollback()
		return 0, serlrt.Error
	}
	tx.Commit()
	return int(data.ID), nil
}
func (*Orders) OrdersAdds(data *Orders) (int, error) {
	tx := DB.Begin()
	serlrt := DB.Debug().Create(&data)
	if serlrt.Error != nil {
		tx.Rollback()
		return 0, serlrt.Error
	}
	tx.Commit()
	return int(data.ID), nil
}

func (*Orders) Orderslist(id int) (orders Orders, err error) {
	err = DB.Where("id=?", id).Find(&orders).Error
	return orders, err
}
